﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AtletIS.Shared.Data.aDbDataSetTableAdapters;
using AtletIS.Shared.Helpers;

namespace AtletIS.Shared.Forms.Signups
{
    public partial class frmSignupsFind : Form
    {
        private PerformancesTableAdapter taPerformances = new PerformancesTableAdapter();
        private DataTable dtPerformances;

        // Constructor
        public frmSignupsFind()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.AtletIS64;
        }

        // Form Load
        private void frmFind_Load(object sender, EventArgs e)
        {
            this.lblTitle.Text = Globals.ActiveCompetition.Name;
            this.txtStNum.Focus();
        }

        // btnFind Click
        private void btnFind_Click(object sender, EventArgs e)
        {
            if (rbStNum.Checked)
            {
                try
                {
                    dtPerformances = DatabaseHelper.GetPerformancesByCompetitionIdStartNumber(Globals.ActiveCompetition.ID, Convert.ToInt32(txtStNum.Text)); 
                    // taPerformances.GetByStartNumberCompetitionId(Convert.ToInt32(txtStNum.Text), Globals.ActiveCompetition.ID);
                    dgvSignups.DataSource = dtPerformances;
                    Helpers.DgvLayoutHelper.FindSignupLayout(dgvSignups);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Vyhľadanie", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            
            if (rbSurname.Checked)
            {
                try
                {
                    dtPerformances = DatabaseHelper.GetPerformancesByCompetitionIdLikeSurname(Globals.ActiveCompetition.ID, "%" + txtSurname.Text + "%"); 
                    // taPerformances.GetLikeSurnameCompetitionId("%" + txtSurname.Text + "%", Globals.ActiveCompetition.ID);
                    dgvSignups.DataSource = dtPerformances;
                    Helpers.DgvLayoutHelper.FindSignupLayout(dgvSignups);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Vyhľadanie", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }

            dgvSignups.ReadOnly = true;
        }

        // Find st.number
        private void rbStNum_CheckedChanged(object sender, EventArgs e)
        {
            txtStNum.Focus();
            txtStNum.Enabled = true;
            txtSurname.Enabled = false;
        }

        // Find surname
        private void rbSurname_CheckedChanged(object sender, EventArgs e)
        {
            txtSurname.Focus();
            txtSurname.Enabled = true;
            txtStNum.Enabled = false;
        }

        



    }
}
